﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Xml;

namespace PwjCy.Lite.Storage.Imp
{
    public abstract class AbstractStatement : IStatement
    {
        protected IDbDataAdapter _adapter;
        protected IDbCommand _command;
        protected IDbTransaction _transaction;

        protected AbstractStatement(IDbCommand cmd)
        {
            this._command = cmd;
        }
        public virtual IStatement Clone(IConnection conn, string originalTableName, string newTableName, bool replaceAllTableName)
        {
            throw new NotImplementedException();
        }

        protected virtual IStatement CreateInstance(IConnection conn, string sql)
        {
            throw new NotImplementedException();
        }
        public virtual DataSet ExecuteDataSet()
        {
            try
            {
                using (_command.Connection)
                {
                    _command.Connection.Open();
                    DataSet dataSet = new DataSet();
                    _adapter.Fill(dataSet);
                    _command.Parameters.Clear();
                    return dataSet;
                }
            }
            catch (Exception ex)
            {
                //记录日志
                return null;
            }
            finally
            {
                _command.Connection.Close();
            }
        }
        public virtual void ExecuteDataSet(DataSet ds)
        {
            throw new NotImplementedException();
        }
        public virtual int ExecuteNonQuery()
        {
            return this._command.ExecuteNonQuery();
        }
        public virtual IResultReader ExecuteReader()
        {
            throw new NotImplementedException();
        }
        public virtual IDataReader ExecuteReader(CommandBehavior cb)
        {
            return _command.ExecuteReader(cb);
        }
        public virtual IDataReader ExecuteReaderDirectly()
        {
            return _command.ExecuteReader();
        }
        public virtual object ExecuteScalar()
        {
            return _command.ExecuteScalar();
        }
        public abstract XmlReader ExecuteXmlReader();
        public virtual string GetPasswordEscapedConnectionString()
        {
            throw new NotImplementedException();
        }
        protected abstract void InitializeAdapter();
        private string GetConnectionString(string str)
        {
            return _command.Connection.ConnectionString;
        }
        public string CommandText
        {
            get
            {
                if (null != this._command)
                {
                    return this._command.CommandText;
                }
                return null;
            }
        }
        public int CommandTimeout
        {
            get
            {
                return this._command.CommandTimeout;
            }
            set
            {
                this._command.CommandTimeout = value;
            }
        }
        public IDbTransaction Transaction
        {
            get
            {
                return this._transaction;
            }
            set
            {
                this._transaction = value;
                this._command.Transaction = this._transaction;
            }
        }
    }
}
